package tools;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import fr.afcepf.atod11.framework.controller.ActionBean;

public class XmlParser {
	private static XmlParser instance;

	public static XmlParser getInstance() {
		if (instance == null)
			instance = new XmlParser();
		return instance;

	}

	public Map<String, ActionBean> parseConfiguration(String path)
	throws ParserConfigurationException, SAXException, IOException {
		Map<String, ActionBean> result = new HashMap<String, ActionBean>();

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();

		Document document = builder.parse(path);
		Element root = document.getDocumentElement();
		NodeList listActions = root.getElementsByTagName("action");

		for (int i = 0; i < listActions.getLength(); i++){
			Node currentNode= listActions.item(i);
			NodeList currentList = currentNode.getChildNodes();
			ActionBean newAction = new ActionBean();
			for (int j = 0; j < currentList.getLength(); j++)
			{
				Node child = currentList.item(j);
				if (child.getNodeName().equals("action-name"))
					newAction.setActionName(child.getTextContent());
				if (child.getNodeName().equals("url-pattern"))
					newAction.setUrlPattern(child.getTextContent());
				if(child.getNodeName().equals("form-name"))
					newAction.setFormName(child.getTextContent());
			}
			System.out.println("NewAction " + newAction.getActionName() + " " + newAction.getFormName() + " " + newAction.getUrlPattern());
			result.put(newAction.getUrlPattern(), newAction);
		}

		Iterator it2 = result.keySet().iterator();
		while (it2.hasNext()) {
			System.out.println("Action : " + it2.next());
		}

		NodeList listForms = root.getElementsByTagName("form");

		for (int i = 0; i < listForms.getLength(); i++){
			Node currentNode= listForms.item(i); //<form>
			Element element = (Element)currentNode;
			NodeList currentList = currentNode.getChildNodes(); //form-class et form-name

			for (int j = 0; j < currentList.getLength(); j++)
			{

				Node child = currentList.item(j);
				String currentFormName=null;
				if (child.getNodeName().equals("form-name"))
					currentFormName = child.getTextContent();

				Iterator it = result.entrySet().iterator();
				while (it.hasNext()) {
					Map.Entry action1 = (Map.Entry)it.next();
					ActionBean action = (ActionBean)action1.getValue();
					if(action.getFormName().equals(currentFormName))
						action.setFormClassName(element.getElementsByTagName("form-class").item(0).getTextContent());
				}
			}
		}

		for (ActionBean ab : result.values()) {
			System.out.println("Action " + ab.getActionName() + " " + ab.getFormName() + " " + ab.getUrlPattern() + " " + ab.getFormClassName());
		}
		return result;
	}

}
